/**
 * 
 */
package com.cipe.cmrs.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.AccountCode;
import com.cipe.cmrs.service.AccountCodeService;

/**
 * @author Incepio
 */
@Controller
public class AccountCodeController extends BaseController {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -3823562098119817733L;
	@Autowired
	private AccountCodeService	accountCodeService;

	@RequestMapping("/secure/saveAccountCode.do")
	public String saveAccountCode(@ModelAttribute("accountCode") AccountCode accountCode, BindingResult result, Model model, HttpServletRequest request) {
		try {
			accountCode = accountCodeService.saveAccountCode(accountCode, getUserId(request));
			saveMessage(request, getText("editAccountCode.message.success"));
		} catch (Exception e) {
			logger.error(getText("error.transaction"), e);
			saveError(request, getText("error.transaction"));
		}
		model.addAttribute("accountCode", accountCode);
		return "editAccountCode";
	}

	@RequestMapping("/secure/showAccountCodeList.do")
	public String showAccountCodeList(Model model) {
		model.addAttribute("accountCodes", accountCodeService.findAll());
		return "accountCodeList";
	}

	@RequestMapping("/secure/showAddAccountCode.do")
	public String showAddAccountCode(Model model) {
		model.addAttribute("accountCode", new AccountCode());
		return "editAccountCode";
	}

	@RequestMapping("/secure/showEditAccountCode.do")
	public String showEditAccountCode(@RequestParam Integer id, Model model) {
		model.addAttribute("accountCode", accountCodeService.findById(id));
		return "editAccountCode";
	}
}
